﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VideoRentalShop.View.Panel;
using VideoRentalShop.Controller;
using VideoRentalShop.View.Templates;

namespace VideoRentalShop.View
{
    /// <summary>
    /// Logika interakcji dla klasy MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static readonly Brush BRUSH_HIGHLIGHTED_BUTTON = new SolidColorBrush(Color.FromArgb(100, 255, 255, 255));
        private static readonly Brush BRUSH_SELECTED_BUTTON = new SolidColorBrush(Color.FromArgb(50, 255, 255, 255));
        private static readonly Brush BRUSH_NORMAL_BUTTON = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
        private Brush _oldBrush;
        private FrameworkElement[] _panels;
        private Button[] _buttons;
        private int visiblePanel = -1;
        private VideoRentalShopController Controller { get; set; }


        public MainWindow()
        {
            Controller = VideoRentalShopController.Instance;
            InitializeComponent();

            var panRent = new StepPanel("Wypożyczenie filmów");
            panRent.AddPanel(new VideoRentalShop.View.Panel.RentStepPanel.ChooseClientStepPanel { }, "Wybierz klienta");
            panRent.AddPanel(new VideoRentalShop.View.Panel.RentStepPanel.ChooseMoviesStepPanel { }, "Wybierz filmy");
            panRent.AddPanel(new VideoRentalShop.View.Panel.RentStepPanel.RentSummaryStepPanel { }, "Podsumowanie");
            var panReturn = new StepPanel("Oddanie filmów");
            panReturn.AddPanel(new VideoRentalShop.View.Panel.ReturnStepPanel.ChooseClientStepPanel { }, "Wybierz klienta");
            panReturn.AddPanel(new VideoRentalShop.View.Panel.ReturnStepPanel.ChooseMoviesStepPanel { }, "Wybierz filmy");
            panReturn.AddPanel(new VideoRentalShop.View.Panel.ReturnStepPanel.ReturnSummaryStepPanel { }, "Podsumowanie");
            
            var panManageClient = new VideoRentalShop.View.Windows.ClientList.ClientListPanel();
            var panManageMovie = new VideoRentalShop.View.Windows.MovieList.MovieListPanel();
            var panManageOrder = new VideoRentalShop.View.Windows.OrderList.OrderListPanel();

            _panels = new FrameworkElement[] { panRent, panReturn, panManageMovie, panManageClient, panManageOrder };
            _buttons = new Button[] { btnRent, btnReturn, btnManageMovie, btnManageClient, btnManageOrder };
            ConfigurePanels();
            ChangePanel(0);
        }


        private void ConfigurePanels()
        {
            for (var i = 0; i < _buttons.Length; ++i)
            {
                _buttons[i].Tag = i;
                _buttons[i].TabIndex = 10000 + i;
            }

            var idx = 0;
            foreach (var element in _panels)
            {
                element.Margin = new Thickness(0d);
                element.VerticalAlignment = VerticalAlignment.Stretch;
                element.HorizontalAlignment = HorizontalAlignment.Stretch;
                element.Width = gridContent.Width;
                element.Height = gridContent.Height;
                element.Visibility = Visibility.Collapsed;
                element.Tag = idx++;
                gridContent.Children.Add(element);
            }
        }

        private void ChangePanel(int idx)
        {
            bool canHide = true;
            if (visiblePanel >= 0 && _panels[visiblePanel] is IHidible)
            {
                canHide = (_panels[visiblePanel] as IHidible).CanHide;
            }

            if (canHide && (idx != visiblePanel && idx >= 0 && idx < _panels.Length))
            {
                _panels[idx].Visibility = Visibility.Visible;
                ChangeButtonBackground(_buttons[idx], BRUSH_SELECTED_BUTTON);
                if (visiblePanel >= 0)
                {
                    _panels[visiblePanel].Visibility = Visibility.Collapsed;
                    ChangeButtonBackground(_buttons[visiblePanel], BRUSH_NORMAL_BUTTON);
                }

                visiblePanel = idx;
            }
        }

        private void ChangeButtonBackground(Button button, Brush background)
        {
            if (button.Background == BRUSH_HIGHLIGHTED_BUTTON)
            {
                _oldBrush = background;
            }
            else
            {
                button.Background = background;
            }
        }


        private void MenuButtonMouseEnter(object sender, MouseEventArgs e)
        {
            var control = ((Control)sender);
            _oldBrush = control.Background;
            control.Background = BRUSH_HIGHLIGHTED_BUTTON;
        }

        private void MenuButtonMouseLeave(object sender, MouseEventArgs e)
        {
            var control = ((Control)sender);
            control.Background = _oldBrush;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void ChangePanelClick(object sender, RoutedEventArgs e)
        {
            var control = ((Control)sender);
            var tag = (control.Tag != null) ? control.Tag.ToString() : string.Empty;
            var idx = Int32.Parse(tag);
            ChangePanel(idx);
        }
    }
}
